package com.top10.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.top10.common.Const;
import com.top10.entity.*;

import com.top10.entity.vo.BrowseVo;
import com.top10.entity.vo.UserVo;
import com.top10.mapper.UserMapper;
import com.top10.service.IGoodsDetailedService;
import com.top10.service.ISpecialPriceService;
import com.top10.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ellen
 * @since 2018-09-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    private IGoodsDetailedService iGoodsDetailedService;
    @Autowired
    private ISpecialPriceService iSpecialPriceService;

    @Override
    public List<Shop> selectShopCollect(String userUuid) {return userMapper.selectShopCollect(userUuid);}

    @Override
    public List<Goods> selectGoodsCollect(String userUuid) {
        return userMapper.selectGoodsCollect(userUuid);
    }

    @Override
    public UserVo selectUserVo(String uuid) {
        return userMapper.selectUserVo(uuid);
    }

    @Override
    public List<BrowseVo> userBrowse(String userUuid) {
        List<BrowseVo> browseVos = userMapper.userBrowse(userUuid);
        for(BrowseVo browseVo: browseVos){
            GoodsDetailed goodsDetailed=iGoodsDetailedService.selectOne(new EntityWrapper<GoodsDetailed>().eq("goods_uuid",browseVo.getUuid()));
            SpecialPrice specialPrice3=iSpecialPriceService.selectOne(new EntityWrapper<SpecialPrice>()
                    .eq("isdeleted", Const.SpecialIsNotDeleted)
                    .eq("goods_uuid",browseVo.getUuid())
                    .gt("end_time",new Date()));
            if(goodsDetailed!=null && specialPrice3==null){
                browseVo.setType(String.valueOf(0));
            }
            if(goodsDetailed!=null && specialPrice3!=null){
                SpecialPrice specialPrice=iSpecialPriceService.selectOne(new EntityWrapper<SpecialPrice>()
                        .eq("type",1)
                        .eq("isdeleted", Const.SpecialIsNotDeleted)
                        .eq("goods_uuid", browseVo.getUuid())
                        .gt("end_time",new Date()));
                SpecialPrice specialPrice1=iSpecialPriceService.selectOne(new EntityWrapper<SpecialPrice>()
                        .eq("type",2)
                        .eq("isdeleted", Const.SpecialIsNotDeleted)
                        .eq("goods_uuid", browseVo.getUuid())
                        .gt("end_time",new Date()));
                if(specialPrice!=null){
                    //多件惠
                    browseVo.setType(String.valueOf(1));
                }
                if(specialPrice1!=null){
                    //闪购
                    browseVo.setType(String.valueOf(2));
                }
            }
        }

        return browseVos;
    }
    @Override
    public Integer insertOne(User user){
        return userMapper.insert(user);
    }

    @Override
    public  Integer updateOne(User user){
        return userMapper.update(user,new EntityWrapper<User>().eq("account",user.getAccount()));
    }
    @Override
    public String getHeadSculpture(String user_uuid){
        return userMapper.getHeadSculpture(user_uuid);
    }

    @Override
    public User getUser(String account){
        return userMapper.getUser(account);
    }

    @Override
    public Integer deleteBank(String userUuid){
        Integer rest =userMapper.deleteBank(userUuid);
        return  rest;
    }
    @Override
    public Integer updateBnak(User user){
        Integer rest=userMapper.updateBnak(user);
        return rest;
    }
    @Override
    public Integer updateUser(User user){
        Integer rest=userMapper.updateUser(user);
        return rest;
    }


}
